package com.ecarx.vhaltests.hy11.testcases.charging;


import static com.ecarx.xui.adaptapi.car.hev.ICharging.ChargingDisChargingState.DEFAULT;
import static com.ecarx.xui.adaptapi.car.hev.ICharging.ChargingDisChargingState.DISCHARING;
import static com.ecarx.xui.adaptapi.car.hev.ICharging.ChargingDisChargingState.DISCHARING_END;
import static com.ecarx.xui.adaptapi.car.hev.ICharging.ChargingDisChargingState.DISCHARING_FALUT;
import static com.ecarx.xui.adaptapi.car.property.IProperty.CHARGE_FUNC_BATTERY_DISCHARGING_STATE;

import android.car.VehicleAreaType;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;

import androidx.test.platform.app.InstrumentationRegistry;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase;
import com.ecarx.xui.adaptapi.car.IWrapper;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ChargeFuncBatteryDischargingStateTest extends VhalUnitTestBase {
    private static int propertyId;
    private static IWrapper.IPropertyId wrapperProperty;
    private CarPropertyManager.CarPropertyEventCallback propertyCbk;
    private static IpcpClient ipcpClient = new IpcpClient();
    private boolean isFirstCbk;
    private static final int AREA_ID = VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;

    private final Object cbkLock = new Object();
    private int cbkValue;

    @BeforeClass
    public static void testInit() {
        IWrapper wrapper = com.ecarx.xui.adaptapi.car.Car.createWrapper(InstrumentationRegistry.getInstrumentation().getTargetContext());
        wrapperProperty = wrapper.getWrappedPropertyId(IWrapper.WrappedIdType.ID_TYPE_FUNCTION, CHARGE_FUNC_BATTERY_DISCHARGING_STATE);
        propertyId = wrapperProperty.getPropertyId();
        setAvailable(true);
        Assert.assertTrue(isAvail());
    }

    @Test
    public void test001_GetChargeFuncBatteryDischargingStateDefault() {
        testGet(DEFAULT);
    }

    @Test
    public void test002_GetChargeFuncBatteryDischargingStateDischarging() {
        testGet(DISCHARING);
    }

    @Test
    public void test003_GetChargeFuncBatteryDischargingStateDischargingEnd() {
        testGet(DISCHARING_END);
    }

    @Test
    public void test004_GetChargeFuncBatteryDischargingStateDischargingFalut() {
        testGet(DISCHARING_FALUT);
    }

    @Test
    public void test005_GetChargeFuncBatteryDischargingStateCbk() {
        testCbk();
    }

    @Test
    public void test006_GetChargeFuncBatteryDischargingStateUnavail() {
        setAvailable(false);
        Assert.assertFalse(isAvail());
    }

    private void testGet(int state) {
        mockState(state);
        delay();
        int expectedValue = wrapperProperty.getPropertyValue(state);
        Assert.assertEquals(expectedValue, getValue());
    }

    private static void mockState(Integer state) {
        setAvailable(true, state);
    }

    private static void setAvailable(boolean isAvail) {
        setAvailable(isAvail, null);
    }

    private static void setAvailable(boolean isAvail, Integer state) {
        int cemSvcId = 37;
        int[] cemOperIds = {999};

        int ccSvcId = 135;
        int[] ccOperIds = {200};
        int[] ccConfigs = {13, 623};
        int[] ccValues = {4, 2};

        int timeoutSvcId = 99;
        int[] timeoutOperIds = {200};

        String[] timeoutConfigs = {"chrgnOrDisChrgnStsFbTimeout"};
        int[] timeoutValues = {0};

        ipcpClient.setConfig(cemSvcId, cemOperIds);
        ipcpClient.setConfig(ccSvcId, ccOperIds);
        ipcpClient.setConfig(timeoutSvcId, timeoutOperIds);
        String[] cems = {"vehModMngtGlbSafe1CarModSts1", "vehModMngtGlbSafe1UsgModSts"};
        int[] cemValues = {isAvail ? VehicleConstants.CAR_MODE_NOMAL : VehicleConstants.CAR_MODE_TRANSPORT, VehicleConstants.USG_MODE_CONVENIENCE};
        boolean cemRet = ipcpClient.setCEMSignal(cemSvcId, cemOperIds[0], cems, cemValues);
        Assert.assertTrue(cemRet);
        boolean ccRet = ipcpClient.setCarconfigSignal(ccSvcId, ccOperIds[0], ccConfigs, ccValues);
        Assert.assertTrue(ccRet);
        boolean toRet = ipcpClient.setTimeoutSignal(timeoutSvcId, timeoutOperIds[0], timeoutConfigs, timeoutValues);
        Assert.assertTrue(toRet);
        if (state != null) {
            int vddmSvcId = 48;
            int[] vddmOperIds = {999};
            String[] vddmConfigs = new String[]{"chrgnOrDisChrgnStsFb"};
            ;
            int[] vddmValues = null;
            ipcpClient.setConfig(vddmSvcId, vddmOperIds);
            switch (state) {
                case DEFAULT:
                    vddmValues = new int[]{0};
                    break;
                case DISCHARING:
                    vddmValues = new int[]{8};
                    break;
                case DISCHARING_END:
                    vddmValues = new int[]{9};
                    break;
                case DISCHARING_FALUT:
                    vddmValues = new int[]{12};
                    break;
            }
            if (vddmConfigs != null && vddmValues != null) {
                boolean retVDDM = ipcpClient.setVDDMSignal(vddmSvcId, vddmOperIds[0], vddmConfigs, vddmValues);
                Assert.assertTrue(retVDDM);
            }
        }
    }

    private int getValue() {
        delay();
        return mCarPropertyMgr.getIntProperty(propertyId, AREA_ID);
    }

    private static boolean isAvail() {
        delay();
        return mCarPropertyMgr.isPropertyAvailable(propertyId, AREA_ID);
    }

    private void testCbk() {
        cbkValue = -1;
        mockState(DISCHARING);
        delay();
        int expectedValue = wrapperProperty.getPropertyValue(DISCHARING_FALUT);
        propertyCbk = new CarPropertyManager.CarPropertyEventCallback() {
            @Override
            public void onChangeEvent(CarPropertyValue carPropertyValue) {
                if (carPropertyValue.getAreaId() == AREA_ID) {
                    if (carPropertyValue.getAreaId() == AREA_ID) {
                        if (isFirstCbk) {
                            isFirstCbk = false;
                        } else {
                            cbkValue = (int) carPropertyValue.getValue();
                            unLock();
                        }
                    }
                }
            }

            @Override
            public void onErrorEvent(int i, int i1) {
            }
        };
        mCarPropertyMgr.registerCallback(propertyCbk, propertyId, CarPropertyManager.SENSOR_RATE_ONCHANGE);
        delay();
        mockState(DISCHARING_FALUT);
        lock();
        Assert.assertEquals(expectedValue, cbkValue);
    }

    private void lock() {
        try {
            synchronized (cbkLock) {
                cbkLock.wait(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void unLock() {
        synchronized (cbkLock) {
            cbkLock.notify();
        }
    }

}